import 'package:dio/dio.dart';
import 'package:flower/components/custom_toast.dart';
import 'package:flower/model/base_model.dart';
import 'package:flower/pages/tabs/user/children/address_add.dart';
import 'package:flutter/material.dart';

import '../../../../config/service_url.dart';
import '../../../../model/address_list.dart';
import '../../../../utils/http_util.dart';

/* 收货地址页面 */
class Address extends StatefulWidget {
  const Address({super.key});

  @override
  State<Address> createState() => _AddressListState();
}

class _AddressListState extends State<Address> {
  List<AddressData> addressListData = [];

  @override
  void initState() {
    super.initState();
    queryAddressList();
  }

  void queryAddressList() async {
    Response result = await HttpUtil.get(listAddress);
    AddressListModel addressListModel = AddressListModel.fromJson(result.data);
    setState(() {
      addressListData = addressListModel.data;
    });
  }

  void handleDelete(int id) async {
    Map<String, dynamic> map = <String, dynamic>{};
    map["id"] = id;

    Response result = await HttpUtil.post(deleteAddress, queryParameters: map);

    BaseModel baseModel = BaseModel.fromJson(result.data);

    if (baseModel.code == 1) {
      CustomToast.showSuccessToast("删除成功");
      queryAddressList();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: const Text("收货地址",
            style: TextStyle(fontSize: 16, color: Colors.black)),
        centerTitle: true,
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            Expanded(
                child: ListView.builder(
                    itemCount: addressListData.length,
                    itemBuilder: (BuildContext context, int index) {
                      AddressData data = addressListData[index];
                      var boxDecoration = BoxDecoration(
                          border: Border.all(
                            width: 1,
                            color: Color(int.parse('fa436a', radix: 16))
                                .withAlpha(255),
                          ),
                          borderRadius:
                              const BorderRadius.all(Radius.circular(2)));
                      return Container(
                        padding: const EdgeInsets.all(15),
                        decoration: BoxDecoration(
                            border: Border(
                                top: BorderSide(
                                    width: 1,
                                    color: Color(int.parse('f5f5f5', radix: 16))
                                        .withAlpha(255)))),
                        child: Row(
                          children: [
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    children: [
                                      Visibility(
                                        visible: data.isDefault == 1,
                                        child: Container(
                                          margin:
                                              const EdgeInsets.only(right: 3),
                                          decoration: boxDecoration,
                                          child: Text("默认",
                                              style: TextStyle(
                                                  fontSize: 12,
                                                  color: Color(int.parse(
                                                          'fa436a',
                                                          radix: 16))
                                                      .withAlpha(255))),
                                        ),
                                      ),
                                      Text(data.detail,
                                          style: TextStyle(
                                              fontSize: 15,
                                              color: Color(int.parse('303133',
                                                      radix: 16))
                                                  .withAlpha(255))),
                                    ],
                                  ),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Text("${data.consignee} ${data.phone}",
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(int.parse('909399',
                                                  radix: 16))
                                              .withAlpha(255))),
                                ],
                              ),
                            ),
                            Row(
                              children: [
                                InkWell(
                                  onTap: () {
                                    // 进入添加页
                                    Navigator.of(context).push(
                                      MaterialPageRoute(
                                        builder: (context) => AddressAdd(
                                          addressId: data.id,
                                        ),
                                      ),
                                    );
                                  },
                                  child: Image.asset(
                                    "assets/images/edit.png",
                                    height: 20,
                                    width: 22,
                                  ),
                                ),
                                const SizedBox(
                                  width: 15,
                                ),
                                InkWell(
                                    onTap: () {
                                      // 删除
                                      handleDelete(data.id);
                                    },
                                    child: Image.asset(
                                      "assets/images/delete.png",
                                      height: 20,
                                      width: 22,
                                    )),
                              ],
                            )
                          ],
                        ),
                      );
                    })),
            InkWell(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const AddressAdd(),
                  ),
                );
              },
              child: Container(
                alignment: Alignment.center,
                width: MediaQuery.of(context).size.width,
                height: 50,
                margin: const EdgeInsets.all(15),
                decoration: BoxDecoration(
                  color: Color(int.parse('fa436a', radix: 16)).withAlpha(255),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: const Text(
                  '新增地址',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
